const tokens = {
  admin: {
    token: 'admin-token'
  },
  editor: {
    token: 'editor-token'
  },
  chen: {
    token: 'user-token'
  }
}

const users = {
  'admin-token': {
    roles: ['admin'],
    introduction: 'I am a super administrator',
    avatar: 'https://thesecondclassrjkf.cn/uploads/29ba2af2fb58a9a6f6911148cf11542b',
    name: 'Super Admin'
  },
  'editor-token': {
    roles: ['editor'],
    introduction: 'I am an editor',
    avatar: 'https://thesecondclassrjkf.cn/uploads/29ba2af2fb58a9a6f6911148cf11542b',
    name: 'Normal Editor'
  },
  'user-token': {
    roles: ['chen'],
    introduction: 'I am an user',
    avatar: 'https://thesecondclassrjkf.cn/uploads/29ba2af2fb58a9a6f6911148cf11542b',
    name: '陈昀昊'
  }
}
const data = [
  { 'name': 'fei', 'id': '3171104101', 'password': '123456', 'email': '123456789@qq.com', 'qqNumber': '123456789', 'telephone': '12345678911', 'avatar': '1' },
  { 'name': 'liang', 'id': '3171104102', 'password': '123456', 'email': '123456789@qq.com', 'qqNumber': '123456789', 'telephone': '12345678911', 'avatar': '1' },
  { 'name': 'jun', 'id': '3171104103', 'password': '123456', 'email': '123456789@qq.com', 'qqNumber': '123456789', 'telephone': '12345678911', 'avatar': '1' },
  { 'name': 'ming', 'id': '3171104104', 'password': '123456', 'email': '123456789@qq.com', 'qqNumber': '123456789', 'telephone': '12345678911', 'avatar': '1' }
]
export default [
  // user login
  {
    url: '/admin/login',
    type: 'post',
    response: config => {
      const { username } = config.body
      const token = tokens[username]

      // mock error
      if (!token) {
        return {
          code: 60204,
          message: 'Account and password are incorrect.'
        }
      }

      return {
        code: 20000,
        data: token
      }
    }
  },
  // 获取用户列表
  {
    url: '/users',
    type: 'get',
    response: config => {
      return (
        {
          'infos': {
            list: data
          }
        }
      )
    }
  },
  // 删除指定用户
  {
    url: '/user',
    type: 'delete',
    response: config => {
      const { path } = config
      const id = path.split('/')[3]
      var index
      for (var i in data) {
        if (data[i]._id === id) {
          index = i
          break
        }
      }
      data.splice(index, 1)
      return (
        data
      )
    }
  },
  // 查找指定用户
  {
    url: '/user',
    type: 'get',
    response: config => {
      const { path } = config
      const id = path.split('/')[3]
      var index
      for (var i in data) {
        if (data[i]._id === id) {
          index = i
          break
        }
      }
      return (
        data[index]
      )
    }
  },
  // 保存指定用户
  {
    url: '/user',
    type: 'put',
    response: config => {
      const { name, password, email, qqNumber, telephone, avatar } = config.body
      const { path } = config
      const id = path.split('/')[3]
      var index
      for (var i in data) {
        if (data[i]._id === id) {
          index = i
          data[index].name = name
          data[index].password = password
          data[index].email = email
          data[index].qqNumber = qqNumber
          data[index].telephone = telephone
          data[index].avatar = avatar
          break
        }
      }
      return (
        data[index]
      )
    }
  },
  // 新建用户
  {
    url: '/user',
    type: 'post',
    response: config => {
      console.log(config.query)
      const { _id, name, password, email, qqNumber, telephone, avatar } = config.body
      console.log(name + password + email + qqNumber + telephone + avatar + _id)
      const person = { 'name': name, '_id': _id, 'password': password, 'email': email, 'qqNumber': qqNumber, 'telephone': telephone, 'avatar': avatar }
      data.push(person)
      return (
        data
      )
    }
  },
  // get user info
  {
    url: '/user/info\.*',
    type: 'get',
    response: config => {
      const { token } = config.query
      const info = users[token]

      // mock error
      if (!info) {
        return {
          code: 50008,
          message: 'Login failed, unable to get user details.'
        }
      }

      return {
        code: 20000,
        data: info
      }
    }
  },

  // user logout
  {
    url: '/user/logout',
    type: 'post',
    response: _ => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  }
]
